﻿
(function () {

    var TASKR =
    {

        // adds a task by calling the service and then fires an optional 
        // callback function
        addTask: function (task, callback) {
            $.ajax({
                type: 'GET',
                url: "/Tasks/AddTask",
                data: task,
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    if (callback != undefined) {
                        callback(result);
                    }
                }
            });

        },

        // removes a task by calling the service and then fires an optional 
        // callback function
        removeTask: function (taskId, callback) {
            $.ajax({
                type: 'GET',
                url: '/Tasks/DeleteTask',
                data: { taskId: taskId },
                success: function () {
                    if (callback != undefined) {
                        callback();
                    }
                }
            });
        },

        // calls a method on the service that sets or nullifies the completed
        // date of the specified task, thus rendering it complete or not        
        toggleTaskCompletion: function (taskId, completed, callback) {
            if (completed != undefined && completed != '') {
                $.ajax({
                    type: 'GET',
                    url: '/Tasks/ReOpenTask',
                    data: { taskId: taskId },
                    success: function () {
                        if (callback != undefined) {
                            callback();
                        }
                    }
                });
            } else {
                $.ajax({
                    type: 'GET',
                    url: '/Tasks/CompleteTask',
                    data: { taskId: taskId },
                    success: function () {
                        if (callback != undefined) {
                            callback();
                        }
                    }
                });
            }

        },

        // retrieves all tasks from the service via ajax
        listTasks: function (callback, userId) {

            if (userId == undefined) {
                $.ajax({
                    type: "GET",
                    url: "/Tasks/ListAllTasks",
                    dataType: "json",
                    success: function (result) {
                        if (callback != undefined) {
                            callback(result);
                        }
                    }
                });
            }
            else {
                $.ajax({
                    type: "GET",
                    url: "/Tasks/ListUserTasks",
                    dataType: "json",
                    data: { userId: userId },
                    success: function (result) {
                        if (callback != undefined) {
                            callback(result);
                        }
                    }
                });
            }

        },

        listTaskActivity: function (callback, taskId) {            
            $.ajax({
                type: "GET",
                url: "/Tasks/ListTaskActivity",
                dataType: "json",
                data: { taskId: taskId },
                success: function (result) {
                    if (callback != undefined) {
                        callback(result, taskId);
                    }
                }
            });
        }




    };

    if (!window.Taskr) { window.Taskr = TASKR; }

})();